home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / buildlib.1 / buildlib / usr / local / bin / buildlib.pl
Encoding:
Perl Script  |  1994-12-26  |  9.4 KB  |  326 lines

  1. #!/usr/bin/perl -w
  2. # universal shlib builder v.0.1
  3. #
  4. #     - Patrick Nguyen (pnguyen@elde.epfl.ch) (aka MegaFork)
  5. # my 2nd perl script (using perl5).
  6. #*****************************************************************************#
  7. #*    This program has been written by Patrick Nguyen                 *#
  8. #* You may distribute, copy, modify, use this program in any manner you wish *#
  9. #* providing that:                                 *#
  10. #*     - You hold the author harmless for any damage this program may cause *#
  11. #*      - You  charge  no fee  for use,  distribution,  work,  installation, *#
  12. #*        customization or anything that is related to this program or its *#
  13. #*        derivates.                                 *#
  14. #*    - This notice is always included as-is,  with no change of any kind, *#
  15. #*       in any distribution, extract or any human-readable form.          *#
  16. #*    - Any modification you brought to this  program is commented as one, *#
  17. #*       and signed.                                 *#
  18. #*    - The name of  Patrick Nguyen  may  be used to  neither endorse  nor *#
  19. #*       promote  products derived  from this software  without  specific  *#
  20. #*       prior  written permission.                         *#
  21. #*                                         *#
  22. #* Please note that I, as the author, may change the program without adding  *#
  23. #* any comment or notice.                             *#
  24. #* This  program is  distributed  in the  hope that it will be useful,  but  *#
  25. #* without  any  warrantee or  guarantee  of  any  kind,  neither  explicit  *#
  26. #* nor implied, not even this of merchantability and use.             *#
  27. #*                    Patrick Nguyen.                 *#
  28. #* My address:                                     *#
  29. #*  Patrick Nguyen                                 *#
  30. #*  7, ch. de la Goumoenche                             *#
  31. #*  CH-1027 Lonay ( Switzerland )                         *#
  32. #* or pnguyen@elde.epfl.ch if you're inet.                     *#
  33. #*****************************************************************************#
  34. # ACKNOWLEDGEMENTS:
  35. #  This script is heavily based on a shell script written by Hermann Dunkel
  36. # <hedu@cul-ipn.uni-kiel.de>, itself based on a logfile by Jens Claussen
  37. # <claussen@db.fmi.uni-passau.de>. An updated version by myself can be found
  38. # in the package wxWin_shlib, which you should find at your favorite linux
  39. # site.
  40. #
  41. # the initials `mf' should hereinafter refer as me (MegaFork).
  42. #
  43. #--------------------------------------------------------
  44. # editable variables
  45. # ~~~~~~~~~~~~~~~~~~
  46. # The basename for the library:
  47. $NAME="gmp";
  48.  
  49. # Version:
  50. # suffix for the shared lib
  51. $VERSION="1.3.2";
  52.  
  53. # Where should the library be installed?
  54. $DEST_DIR="/usr/local/lib";
  55.  
  56. # X11 support: do we need that?
  57. $NEED_X11=0;
  58. # define this if you need X11
  59. $X11_DIR="/usr/X11R6";
  60.  
  61. # Additionnal libraries (to be passed to the linker).
  62. $ADD_LIBS="";
  63.  
  64. #use whatever value your favorite shlib tables maintainer told you
  65. # Address:
  66. $ADDR="0x67000000";
  67. # J_SIZE: seems reasonable to me -mf
  68. $J_SIZE="0x4000";
  69. # G_SIZE: seems reasonable to me -mf
  70. $G_SIZE="0x2000";
  71.  
  72. # Compiler:
  73. # (would anyone use cc anyway?)
  74. # Add --version if you wish..    -mf
  75. $CC="gcc";
  76.  
  77. # Leave this as is if you don't know
  78. $ROOTDIR=qx{ pwd };
  79. chop($ROOTDIR);
  80.  
  81. #-------------------------------- end of editable area
  82.  
  83. # First, check out the table_description to see whether we are attempting to
  84. # override a shlib.
  85. check_address();
  86.  
  87. # Set the jump directory (where the jump tables shall reside
  88. $JUMP_DIR="$ROOTDIR/jump";
  89. $JUMP_LIB="lib$NAME";
  90. # Setting environnement variables, for use of the dll tools
  91. $ENV{JUMP_DIR}=$JUMP_DIR;
  92. $ENV{JUMP_LIB}=$JUMP_LIB;
  93.  
  94. # The shared libraries specific options that should be passed to `make'.
  95. $SHOPT="CC=\"$CC -O6 -m486 -funroll-loops -D_LINUX_DLL -B/usr/dll/jump/\"";
  96.  
  97. # add /usr/dll/bin to our path
  98. $ENV{PATH}="/usr/dll/bin:$ENV{PATH}";
  99.  
  100. # if the jump directory doesn't exist, create it
  101. if (! -d $JUMP_DIR) {
  102.     mkdir($JUMP_DIR, 0755);
  103. }
  104. chdir("$JUMP_DIR");
  105.  
  106. print "ROOTDIR=$ROOTDIR\n";
  107. print "JUMP_DIR=$JUMP_DIR JUMP_LIB=$JUMP_LIB\n";
  108.  
  109. if (! -d "/usr/dll/bin") {
  110.     print "Could not find /usr/dll/bin!\n";
  111.     print "Is tools-2.x x>=11 installed ?\n";
  112.     exit 1;
  113. }
  114.  
  115. #
  116. # find the existing libs ...
  117. #
  118. $LIB_C="/usr/lib/libc.sa";
  119. $LIB_M="/usr/lib/libm.sa";
  120.  
  121. if ($NEED_X11) {
  122.   $LIB_X11="/usr/lib/libX11.sa";
  123.   $LIB_XT="/usr/lib/libXt.sa";
  124.  
  125.   if (! -f $LIB_X11) {
  126.     $LIB_X11="$X11_DIR/lib/libX11.sa";
  127.   }
  128.   if (! -f $LIB_X11) {
  129.     die "Could not find libX11.sa";
  130.   }
  131.   if (! -f $LIB_XT) {
  132.     $LIB_XT="$X11_DIR/lib/libXt.sa";
  133.   }
  134.   if (! -f $LIB_XT) {
  135.     die "Could not find libXt.sa";
  136.   }
  137.   print "X11: using $LIB_X11 and $LIB_XT\n";
  138. }
  139.  
  140. print "Using $LIB_C $LIB_M\n";
  141.  
  142. chop($LIB_GCC=qx{ find /usr/lib/gcc-lib -name '2.*' -print});
  143.  
  144. die "Could not find libgcc!" unless length($LIB_GCC)!=0;
  145. print "LIB_GCC is $LIB_GCC\n";
  146.  
  147. #
  148. print "The following files are in JUMP_DIR:\n";
  149. system("ls -aC");
  150. print "Press RETURN to continue\n";
  151. <>;
  152.  
  153. $laststage="";
  154.  
  155. for(;;) {
  156.    local $cmd;
  157.    chdir($ROOTDIR);
  158.    print "q)uit\n";
  159.    print "l)ist files in JUMP_DIR cj) clean jumpdir cl) clean lib* cs) clean *.s\n\n";
  160.    print "1) compile stage 1: make clean; make\n";
  161.    print "1a)retry   stage 1: make\n";
  162.    print "2) build jump.import\n";
  163.    print "3) compile stage 2: make clean; make\n";
  164.    print "3a)retry   stage 2: make\n";
  165.    print "4) build shared library\n";
  166.    print "5) install in $DEST_DIR/\n";
  167.    print "\nlaststage was: $laststage\n";
  168.  
  169.    $_=<>;
  170.    $cmd=$_;
  171.    SWITCH: {
  172.     if (/^cs\n/) { system("rm -f *.s");
  173.          print "In JUMP_DIR are:\n";
  174.          system("ls -aC $JUMP_DIR");
  175.          last SWITCH;
  176.     }
  177.     if (/^cl\n/) { system("rm -f $JUMP_DIR/*");
  178.          print "In JUMP_DIR are:\n";
  179.          system("ls -aC $JUMP_DIR");
  180.          last SWITCH;
  181.     }
  182.     if (/^cj\n/) { system("rm -f $JUMP_DIR/*");
  183.          print "In JUMP_DIR are:\n";
  184.          system("ls -aC $JUMP_DIR/");
  185.          last SWITCH;
  186.     }
  187.     if (/^l\n/)  { print "In JUMP_DIR are:\n";
  188.          system("ls -aC $JUMP_DIR");
  189.          last SWITCH;
  190.     }
  191.     if (/^1\n/)  { system("pwd");
  192.     print "compile stage 1, logged to make.log\n" ;
  193.     system("make clean");
  194.     system("make $SHOPT all 2>&1 | tee make.log");
  195.     system("less make.log");
  196.     last SWITCH;
  197.     }
  198.     if (/^1a\n/) {
  199.     print "compile stage 1, retry, logged to make.log\n";
  200.         system("make $SHOPT all 2>&1 | tee make.log");
  201.     system("less make.log");
  202.     last SWITCH;
  203.     }
  204.     if (/^2\n/) {
  205.     chdir($JUMP_DIR);
  206.     print "generating list of imported/ignored Symbols (jump.import)\n";
  207.     system("getvars");
  208.     system("getfuncs");
  209.     print "$LIB_C\n";
  210.     system("nm --no-cplus -o $LIB_C |grep __GOT_ | sed 's/__GOT_/_/' >jump.import");
  211.     if ($NEED_X) {
  212.     print "$LIB_X\n";
  213.         system("nm --no-cplus -o $LIB_X |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import");
  214.     print "$LIB_XT\n";
  215.         system("nm --no-cplus -o $LIB_XT |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import");
  216.     }
  217.         print "$LIB_M\n";
  218.         system("nm --no-cplus -o $LIB_M |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import");
  219.     last SWITCH;
  220.     }
  221.     if (/^3\n/) {
  222.     print "compile stage 2: make clean;make\n";
  223.         system("make clean");
  224.         system("make $SHOPT ) 2>&1 | tee make.log");
  225.     system("less make.log");
  226.     last SWITCH;
  227.     }
  228.     if (/^3a\n/) {
  229.     print "retry stage 2: make\n";
  230.         system("make $SHOPT 2>&1 | tee make.log");
  231.     system("less make.log");
  232.     last SWITCH;
  233.     }
  234.     if (/^4\n/) {
  235.     local $MAGIC_NUM;
  236.     chdir($JUMP_DIR);
  237.     print "mkimage\n";
  238.         # getsize, build the image and lib
  239.     system("getsize >jump.vars-new");
  240.     system("mv -f jump.vars jump.vars-old");
  241.     system("mv jump.vars-new jump.vars");
  242.     # create image and stubs
  243.     $MAGIC_NUM="-v $VERSION -a $ADDR -g $G_SIZE -j $J_SIZE";
  244.     if( $NEED_X ) {
  245.         system("mkimage -f -l $JUMP_LIB $MAGIC_NUM -- -L$LIB_GCC -L$X11_DIR/lib $ROOTDIR/$JUMP_LIB.a $ADD_LIBS -lXt -lX11 -lg++ -lm -lgcc -lc");
  246.     } else {
  247.         system("mkimage -f -l $JUMP_LIB $MAGIC_NUM -- -L$LIB_GCC $ROOTDIR/$JUMP_LIB.a $ADD_LIBS -lg++ -lm -lgcc -lc 2>&1 |less");
  248.     }
  249.     system("mkstubs -l $JUMP_LIB $MAGIC_NUM -- $JUMP_LIB");
  250.     last SWITCH;
  251.     }
  252.     if (/^5\n/) {
  253.     chdir($JUMP_DIR);
  254.     print "Installing in $DEST_DIR (using mv)\n";
  255.     system("mv $JUMP_LIB.so.$VERSION $DEST_DIR");
  256.     system("mv $JUMP_LIB.sa $DEST_DIR");
  257.     system("ldconfig -n $DEST_DIR");
  258.     last SWITCH;
  259.     }
  260.     if (/^q/) {
  261.     print "Goodbye\n";
  262.     exit 0;
  263.     }
  264.     print "wrong command";
  265.     $cmd=$laststage; # save last stage
  266.     last SWITCH;
  267.   } # SWITCH
  268.   $laststage=$cmd
  269. } #main loop
  270.  
  271. # check_address: find out whether the address is already used, or the
  272. # official library resides in another address.
  273. # We should check /etc/ld.so.conf
  274. sub check_address {
  275.   local $libname = "";
  276.   local $addr = 0;
  277.   local $end = 0;
  278.   local $my_addr = hex($ADDR);
  279.   local $my_end = $my_addr + hex($G_SIZE) + hex($J_SIZE);
  280.   local $my_libname = "lib$NAME.so";
  281.  
  282.   open( TABLE_DESCRIPTION, "grep -v ^# /usr/dll/table_description|" );
  283.  
  284.   while( <TABLE_DESCRIPTION> ) {
  285.      chop;
  286.      SWITCH: {
  287.         if (s/^Library://) {
  288.         if( $addr != 0 ) {
  289.           # do we have the same name?
  290.           if( index($my_libname, $libname) != -1 ) {
  291.             print "Found library in description table!\n";
  292.             if( $addr == $my_addr ) {
  293.                print "Same starting address...\n";
  294.             } else {
  295.                print "Address does not match!\n";
  296.                print "Wanted $my_addr, got $addr.\n";
  297.             }
  298.             if ($end == $my_end) {
  299.                print "Same end address...\n";
  300.             } else {
  301.                print "Ending addresses do not match!\n";
  302.                print "Wanted $my_end, $end.\n";
  303.             }
  304.           } elsif ( not (
  305.             (($my_addr < $addr) and ($my_end < $addr)) or
  306.             (($my_addr > $end ) and ($my_end > $end ))
  307.                )) {
  308.              print "Warning!! Possible conflict with $libname!\n";
  309.           }
  310.         }
  311.         $libname=$_;
  312.                 last SWITCH;
  313.         }
  314.         if (s/^Start://) {
  315.         $addr=hex($_);
  316.         }
  317.         if (s/^End//) {
  318.         $end=hex($_);
  319.                 last SWITCH;
  320.         }
  321.      } #  SWITCH
  322.   }    #  while
  323. }      #  find_collision
  324.  
  325.